﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Beetle.Sl.EditorControl.Core;
using Beetle.Sl.EditorControl.Controls;

namespace Beetle.Sl.EditorControl.ToolBars.SetFontFamily
{
    [ToolBarItem]
    public class SetFontFamilyToolBar : ToolBarItem
    {
        public const string COMMANDTEXT = "SetFontStyle";
        private TextBlock txtFontFamily = null;
        private UIElement _content;
        private UIElement _popupContent;

        private string fontNameTemp = string.Empty;

        public override string Name
        {
            get
            {
                return "SetFontFamily";
            }
        }

        public override ToolBarItemVisualTypeEnum ToolBarItemVisualType
        {
            get
            {
                return ToolBarItemVisualTypeEnum.DropDownButton;
            }
        }

        public override UIElement Content
        {
            get
            {
                if (this._content == null)
                {
                    Border border = new Border();
                    border.Margin = new Thickness(1);
                    border.Background = new SolidColorBrush(Colors.White);

                    this.txtFontFamily = new TextBlock();
                    this.txtFontFamily.Width = 60;
                    this.txtFontFamily.VerticalAlignment = VerticalAlignment.Center;
                    this.txtFontFamily.FontSize = 12;
                    this.txtFontFamily.Margin = new Thickness(8, 0, 0, 0);
                    border.Child = this.txtFontFamily;

                    this._content = border;
                }

                return this._content;
            }
        }

        public override UIElement PopupContent
        {
            get
            {
                if (this._popupContent == null)
                {
                    FontFamilySelectPanel fontSizeSelectPanel = new FontFamilySelectPanel();

                    if (this.Editor.FontFamilyNames != null)
                    {
                        System.Collections.ObjectModel.ObservableCollection<FontFamilyInfo> fontFamilyNames = new System.Collections.ObjectModel.ObservableCollection<FontFamilyInfo>();

                        foreach (var item in this.Editor.FontFamilyNames)
                        {
                            FontFamilyInfo fontFamilyInfo = item as FontFamilyInfo;

                            if (fontFamilyInfo != null)
                            {
                                fontFamilyNames.Add(fontFamilyInfo);
                            }
                        }

                        fontSizeSelectPanel.FontFamilyNames = fontFamilyNames;
                    }

                    fontSizeSelectPanel.SelectedValue += new EventHandler(fontSizeSelectPanel_OnSelectedValue);
                    Border border = new Border();
                    border.MaxHeight = 200;
                    border.MinWidth = 140;
                    border.Child = fontSizeSelectPanel;
                    this._popupContent = border;
                }

                return this._popupContent;
            }
        }

        void fontSizeSelectPanel_OnSelectedValue(object sender, EventArgs e)
        {
            this.IsOpenPopup = false;
            this.IsSelected = false;
            FontFamilySelectPanel fontSizeSelectPanel = sender as FontFamilySelectPanel;

            if (fontSizeSelectPanel.Value == null)
            {
                this.txtFontFamily.Text = string.Empty;
                this.fontNameTemp = string.Empty;
            }
            else
            {
                this.fontNameTemp = fontSizeSelectPanel.Value.Name;
                this.txtFontFamily.Text = fontSizeSelectPanel.Value.Description;
                this.txtFontFamily.FontFamily = new FontFamily(this.fontNameTemp);

                this.Editor.ExcuteCommand(SetFontFamilyToolBar.COMMANDTEXT, new object[] { Run.FontFamilyProperty, new FontFamily(fontSizeSelectPanel.Value.Name) });
            }
        }

        public override void OnCommand(object arg)
        {
            if (!string.IsNullOrWhiteSpace(this.fontNameTemp))
            {
                this.Editor.ExcuteCommand(SetFontFamilyToolBar.COMMANDTEXT, new object[] { Run.FontFamilyProperty, new FontFamily(this.fontNameTemp) });
            }
        }

        public override void UpdateVisual()
        {
            if (this.Editor.RichTextBoxInst.IsReadOnly)
            {
                this.IsSelected = false;
                this.IsEnabled = false;
                return;
            }
            else
            {
                this.IsEnabled = true;
            }
        }
    }
}
